package com.qyhl.module_practice.score.mine.shopping;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.module_practice.R;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeScoreMyShoppingBean;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import razerdp.basepopup.BasePopupWindow;

public class PracticeScoreShoppingPop extends BasePopupWindow {
    private View popupView;
    private PracticeScoreMyShoppingBean mData;

    PracticeScoreShoppingPop(Activity context, PracticeScoreMyShoppingBean mData) {
        super(context);
        this.mData = mData;
        bindEvent(context);
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @SuppressLint("InflateParams")
    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.practice_pop_score, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    @SuppressLint("SetTextI18n")
    private void bindEvent(Activity content) {
        ImageView cover = popupView.findViewById(R.id.cover);
        ImageView code = popupView.findViewById(R.id.code);
        ImageView tagAlready = popupView.findViewById(R.id.tag_already);
        TextView title = popupView.findViewById(R.id.title);
        TextView endTime = popupView.findViewById(R.id.get_end_time);
        TextView validTime = popupView.findViewById(R.id.valid_time);
        TextView describe = popupView.findViewById(R.id.describe);
        View codeCover = popupView.findViewById(R.id.code_cover);

        Glide.with(content)
                .load(mData.getItem().getItemPic())
                .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(cover);


        Bitmap image = CodeUtils.createImage(mData.getCode(), StringUtils.dip2px(content, 200), StringUtils.dip2px(content, 200), null);
        Glide.with(content)
                .load(image)
                .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(code);

        endTime.setText("商品兑换时间：" + mData.getCreateTime());

        if (StringUtils.isEmpty(mData.getItem().getExchangeTime())) {
            validTime.setText("截止有效时间：不限期");
        } else {
            validTime.setText("截止有效时间：" + mData.getItem().getExchangeTime());
        }

        title.setText(mData.getItem().getItemName());
        if (mData.isUseState()) {
            tagAlready.setVisibility(View.VISIBLE);
            codeCover.setVisibility(View.VISIBLE);
            title.setTextColor(content.getResources().getColor(R.color.global_gray_lv2));
            describe.setText("已领取，欢迎再次兑换～");
        } else {
            tagAlready.setVisibility(View.GONE);
            codeCover.setVisibility(View.GONE);
            title.setTextColor(content.getResources().getColor(R.color.global_black_lv1));
            describe.setText("请及时到领取点领取");
        }

    }


}
